#include "bsp_writeWold.h"
#include "que.h"
#include <stdio.h>
#include "string.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "driver/uart.h"
#include "bsp_uart.h"
#include "string.h"
#include "bsp_showWorld.h"

void write_task(void){
    
    const char* write_sd_wold[20] = {""};
    for(uint8_t i = 0 ; i < 20 ; i++){
        write_sd_wold[i] = "";
    }//init the buffer of writing
    uint8_t write_wold_index = 0 ;
    uint8_t write_letter_index = 0;
    uint8_t write_wold_page = 0 ;
    char *write_wold_char[20] = {""};
    for(uint8_t i = 0 ; i < 20 ; i++){
        write_wold_char[i] = "";
    }//init the buffer of writing
    bool write_insert_in = false ;
    bool write_to_sd_flag = false;
    uint8_t write_cell[20][20] = {0};
    uint8_t cell_pin_write[6] = {0};
    uint8_t len_of_wold[20] = {0};
    uint8_t total_page_write_sd = 0 ;//写入sd卡的总页数，一次写20，赞够20个再写入
    BrailleCode_t BrailleCode_w[20][20];
    
    for(;;){
        if(write_insert_in == false) {
            EventBits_t ev=xEventGroupWaitBits(button_event_group,BUTTOM_2|BUTTOM_3,pdTRUE,pdTRUE,0);
            if((ev&BUTTOM_2)&&(ev&BUTTOM_3)&&write_insert_in == false) {
                write_insert_in = true;
             }
        }
        if(write_insert_in){
            ESP_LOGI("bsp_writeWold.c","write wold ing------");
            if(write_to_sd_flag == true){
                //write_to_sd(uint8_t total_page_write_sd ,char* write_wold_char[20]){
                ///}
                //write_to_sd(total_page_write_sd++,write_wold_char);
                //写入sd卡，满20个写入jq写
                write_to_sd_flag = false;
            }
/*
            按键使用规则：
           1、 单击1-6按键会写入细胞一个pin
           2、 单击7保存当前写入的6个pin,并且自动切换到下一个字母 注意这里保存的是单个字母！！！
        保存字母会发出蜂鸣器滴一声，并且下方自动显示当前写完的字母，当长度超过六，自动翻页！！
           3、 单击8保存当前写入的单词，这个单词包括若干个cell的六个pin的点数据 注意这里保存的是单个单词，单词包括若干个字母！！
        保存单词会发出蜂鸣器滴滴两声 ，并且下方会清空，重新开始（2）流程
           4、
*/
            EventBits_t ev;
            ev=xEventGroupWaitBits(button_event_group,  BUTTOM_1|
                                                        BUTTOM_2|
                                                        BUTTOM_3|
                                                        BUTTOM_4|
                                                        BUTTOM_5|
                                                        BUTTOM_6,pdFALSE,pdFALSE,0);
            if (ev&BUTTOM_1){
                ESP_LOGE("bsp_writeWold.c","pin 0 is writing in to sd card ing ");
                cell_pin_write[0] = 1;
                xEventGroupClearBits(button_event_group,BUTTOM_1);
                vTaskDelay(200);
            }
            else if (ev&BUTTOM_2){
                ESP_LOGE("bsp_writeWold.c","pin 1 is writing in to sd card ing ");
                cell_pin_write[1] = 1;
                xEventGroupClearBits(button_event_group,BUTTOM_2);
                vTaskDelay(200);
            }
            else if (ev&BUTTOM_3){
                ESP_LOGE("bsp_writeWold.c","pin 2 is writing in to sd card ing ");
                cell_pin_write[2] = 1;
                xEventGroupClearBits(button_event_group,BUTTOM_3);
                vTaskDelay(200);
            }
            else if (ev&BUTTOM_4){
                ESP_LOGE("bsp_writeWold.c","pin 3 is writing in to sd card ing ");
                cell_pin_write[3] = 1;
                xEventGroupClearBits(button_event_group,BUTTOM_4);
                vTaskDelay(200);
            }
            else if (ev&BUTTOM_5){
                ESP_LOGE("bsp_writeWold.c","pin 4 is writing in to sd card ing ");
                cell_pin_write[4] = 1;
                xEventGroupClearBits(button_event_group,BUTTOM_5);
                vTaskDelay(200);
            }
            else if (ev&BUTTOM_6){
                ESP_LOGE("bsp_writeWold.c","pin 5 is writing in to sd card ing ");
                cell_pin_write[5] = 1;
                xEventGroupClearBits(button_event_group,BUTTOM_6);
                vTaskDelay(200);
            }   

            if((ev&BUTTOM_7)&&write_letter_index>=0){//保存当前字母，编辑下一个字母
                ESP_LOGE("bsp_writeWold.c","save the 6 pin ing ");
                xEventGroupClearBits(button_event_group,BUTTOM_7);
                write_cell[write_wold_index][write_letter_index] = cell_pin_write[0]<<5|
                                                                        cell_pin_write[1]<<4|
                                                                        cell_pin_write[2]<<3|
                                                                        cell_pin_write[3]<<2|
                                                                        cell_pin_write[4]<<1|
                                                                        cell_pin_write[5]<<0;
                if(1){//把盲文转换成字母 write_wold_char[write_wold_index][write_letter_index] = 'a';
                    if (write_cell[write_wold_index][write_letter_index] == 32) {
                        write_wold_char[write_wold_index][write_letter_index] = 'a';
                        BrailleCode_w[write_wold_index][write_letter_index] = 32;
                    } else if (write_cell[write_wold_index][write_letter_index] == 48) {
                        write_wold_char[write_wold_index][write_letter_index] = 'b';
                        BrailleCode_w[write_wold_index][write_letter_index] = 48;
                    } else if (write_cell[write_wold_index][write_letter_index] == 36) {
                        write_wold_char[write_wold_index][write_letter_index] = 'c';
                        BrailleCode_w[write_wold_index][write_letter_index] = 36;
                    } else if (write_cell[write_wold_index][write_letter_index] == 38) {
                        write_wold_char[write_wold_index][write_letter_index] = 'd';
                        BrailleCode_w[write_wold_index][write_letter_index] = 38;
                    } else if (write_cell[write_wold_index][write_letter_index] == 34) {
                        write_wold_char[write_wold_index][write_letter_index] = 'e';
                        BrailleCode_w[write_wold_index][write_letter_index] = 34;
                    } else if (write_cell[write_wold_index][write_letter_index] == 52) {
                        write_wold_char[write_wold_index][write_letter_index] = 'f';
                        BrailleCode_w[write_wold_index][write_letter_index] = 52;
                    } else if (write_cell[write_wold_index][write_letter_index] == 54) {
                        write_wold_char[write_wold_index][write_letter_index] = 'g';
                        BrailleCode_w[write_wold_index][write_letter_index] = 54;
                    } else if (write_cell[write_wold_index][write_letter_index] == 50) {
                        write_wold_char[write_wold_index][write_letter_index] = 'h';
                        BrailleCode_w[write_wold_index][write_letter_index] = 50;
                    } else if (write_cell[write_wold_index][write_letter_index] == 20) {
                        write_wold_char[write_wold_index][write_letter_index] = 'i';
                        BrailleCode_w[write_wold_index][write_letter_index] = 20;
                    } else if (write_cell[write_wold_index][write_letter_index] == 22) {
                        write_wold_char[write_wold_index][write_letter_index] = 'j';
                        BrailleCode_w[write_wold_index][write_letter_index] = 22;
                    } else if (write_cell[write_wold_index][write_letter_index] == 40) {
                        write_wold_char[write_wold_index][write_letter_index] = 'k';
                        BrailleCode_w[write_wold_index][write_letter_index] = 40;
                    } else if (write_cell[write_wold_index][write_letter_index] == 56) {
                        write_wold_char[write_wold_index][write_letter_index] = 'l';
                        BrailleCode_w[write_wold_index][write_letter_index] = 56;
                    } else if (write_cell[write_wold_index][write_letter_index] == 44) {
                        write_wold_char[write_wold_index][write_letter_index] = 'm';
                        BrailleCode_w[write_wold_index][write_letter_index] = 44;
                    } else if (write_cell[write_wold_index][write_letter_index] == 46) {
                        write_wold_char[write_wold_index][write_letter_index] = 'n';
                        BrailleCode_w[write_wold_index][write_letter_index] = 46;
                    } else if (write_cell[write_wold_index][write_letter_index] == 42) {
                        write_wold_char[write_wold_index][write_letter_index] = 'o';
                        BrailleCode_w[write_wold_index][write_letter_index] = 42;
                    } else if (write_cell[write_wold_index][write_letter_index] == 60) {
                        write_wold_char[write_wold_index][write_letter_index] = 'p';
                        BrailleCode_w[write_wold_index][write_letter_index] = 60;
                    } else if (write_cell[write_wold_index][write_letter_index] == 62) {
                        write_wold_char[write_wold_index][write_letter_index] = 'q';
                        BrailleCode_w[write_wold_index][write_letter_index] = 62;
                    } else if (write_cell[write_wold_index][write_letter_index] == 58) {
                        write_wold_char[write_wold_index][write_letter_index] = 'r';
                        BrailleCode_w[write_wold_index][write_letter_index] = 58;
                    } else if (write_cell[write_wold_index][write_letter_index] == 28) {
                        write_wold_char[write_wold_index][write_letter_index] = 's';
                        BrailleCode_w[write_wold_index][write_letter_index] = 28;
                    } else if (write_cell[write_wold_index][write_letter_index] == 30) {
                        write_wold_char[write_wold_index][write_letter_index] = 't';
                        BrailleCode_w[write_wold_index][write_letter_index] = 30;
                    } else if (write_cell[write_wold_index][write_letter_index] == 41) {
                        write_wold_char[write_wold_index][write_letter_index] = 'u';
                        BrailleCode_w[write_wold_index][write_letter_index] = 41;
                    } else if (write_cell[write_wold_index][write_letter_index] == 57) {
                        write_wold_char[write_wold_index][write_letter_index] = 'v';
                        BrailleCode_w[write_wold_index][write_letter_index] = 57;
                    } else if (write_cell[write_wold_index][write_letter_index] == 23) {
                        write_wold_char[write_wold_index][write_letter_index] = 'w';
                        BrailleCode_w[write_wold_index][write_letter_index] = 23;
                    } else if (write_cell[write_wold_index][write_letter_index] == 45) {
                        write_wold_char[write_wold_index][write_letter_index] = 'x';
                        BrailleCode_w[write_wold_index][write_letter_index] = 45;
                    } else if (write_cell[write_wold_index][write_letter_index] == 47) {
                        write_wold_char[write_wold_index][write_letter_index] = 'y';
                        BrailleCode_w[write_wold_index][write_letter_index] = 47;
                    } else if (write_cell[write_wold_index][write_letter_index] == 43) {
                        write_wold_char[write_wold_index][write_letter_index] = 'z';
                        BrailleCode_w[write_wold_index][write_letter_index] = 43;
                    }
                        
                    ESP_LOGE("bsp_writeWold.c","save the letter of %c ing ,string is %s",
                                                            write_wold_char[write_wold_index][write_letter_index],
                                                            write_wold_char[write_wold_index]);
                }

                write_letter_index+=1;
                memset(cell_pin_write,0,sizeof(cell_pin_write));
            }
            else if(ev&BUTTOM_8){//保存当前单词，开启下一个单词
                ESP_LOGE("bsp_writeWold.c","save the wold %s ing,leng of wold is %d ",write_wold_char[write_wold_index],write_letter_index);
                xEventGroupClearBits(button_event_group,BUTTOM_8);
                len_of_wold[write_wold_index] = write_letter_index;
                write_letter_index = 0 ;//长度重新设置为0
                if(write_wold_index == 19){
                    write_to_sd_flag = true;
                    write_wold_index = 0 ;
                }
                write_wold_index+=1;//开启下一个单词
                memset(cell_pin_write,0,sizeof(cell_pin_write));
            }
            else if((ev&BUTTOM_9)&&write_letter_index>=0){//重写当前的编辑的字母，当前为保存状态
                ESP_LOGE("bsp_writeWold.c","reset write_wold_index :%d,reset write_letter_index is :%d ,",
                        write_wold_index,write_letter_index);
                memset(cell_pin_write,0,sizeof(cell_pin_write));
                xEventGroupClearBits(button_event_group,BUTTOM_9);
            }
            // else if((ev&BUTTOM_10)&&write_letter_index>=1){//重写当前的编辑的单词 ！这个功能关了
            //     ESP_LOGE("bsp_writeWold.c","reset write_wold_index :%d",write_wold_index);
            //     memset(cell_pin_write,0,sizeof(cell_pin_write));
            //     memset(write_cell[write_wold_index],0,sizeof(write_cell[write_wold_index]));
            //     write_letter_index = 0 ;
            //     xEventGroupClearBits(button_event_group,BUTTOM_10);
            // }
            else if((ev&BUTTOM_10)){//backspace 当前编辑的以及其上一个字母也会被删除
                if(write_letter_index>=1){
                    ESP_LOGE("bsp_writeWold.c","backspace ing");
                    memset(cell_pin_write,0,sizeof(cell_pin_write));
                    write_letter_index--;
                    memset(write_cell[write_wold_index][write_letter_index],0,sizeof(uint8_t));
                    xEventGroupClearBits(button_event_group,BUTTOM_10);
                }
                else if(write_letter_index == 0){
                    write_wold_index-=1;
                    write_letter_index =len_of_wold[write_wold_index];
                }
            }
            show_index_wold_page(write_wold_index,len_of_wold[write_wold_index]/6,len_of_wold,BrailleCode_w);

        }
        vTaskDelay(100);
    }


}

void write_wold_inti(void){
    //xTaskCreatePinnedToCore(write_task, "write_task Task", 2048*5, NULL, 12, NULL,1);
}